﻿using System;
using System.Configuration;
using System.Reflection;
using CtCache;
using CtCommon.Utility;
using Cxx.BLL;
using Cxx.Common;
using Cxx.Entity;
using log4net;

namespace Cxx.OrderManagerSys
{
    public partial class History : System.Web.UI.Page
    {
        private readonly ILog _logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().Name);

        private static readonly int dayslimit = ConfigurationManager.AppSettings["exportDays"].ToInt32();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindList();
            }
        }

        /// <summary>
        /// 绑定列表
        /// </summary>
        private void BindList()
        {
            try
            {
                string condition = MyWebHelper.SelectKey(pnlSearch, out _);
                //logger.Info("筛选信息：" + condition);
                var list = Order.GetListForPage(condition, pager.CurrentPageIndex, pager.PageSize, out var recordCount);
                pager.RecordCount = recordCount;
                rptOrderList.DataSource = list;
                rptOrderList.DataBind();
            }
            catch (Exception e)
            {
                _logger.Error("查询失败：" + e);
                pager.RecordCount = 0;
                rptOrderList.DataSource = null;
                rptOrderList.DataBind();
            }
        }

        public static string GetPaymented(object source)
        {
            bool paymented = (bool) source;
            return paymented ? "已结" : "未结";
        }

        public static string GetHasOrderStr(object source)
        {
            bool paymented = (bool)source;
            return paymented ? "有" : "无";
        }

        protected void btBrowse_Click(object sender, EventArgs e)
        {
            pager.CurrentPageIndex = 1;
            BindList();
        }

        protected void btExportByReceiver_Click(object sender, EventArgs e)
        {
            ExportHandler(ExportMode.ByReceiver);
        }

        protected void btExport_Click(object sender, EventArgs e)
        {
            ExportHandler(ExportMode.OneSheet);
        }

        private void ExportHandler(ExportMode mode)
        {
            if (string.IsNullOrWhiteSpace(txtStartTime.Text) || string.IsNullOrWhiteSpace(txtEndTime.Text))
            {
                JScript.Alert(Page, "导出时需要选择起始时间和结束时间");
                return;
            }
            DateTime startTime = txtStartTime.Text.ToDateTime();
            DateTime endTime = txtEndTime.Text.ToDateTime();
            int days = (int)(endTime - startTime).TotalDays + 1;
            if (days > dayslimit)
            {
                JScript.Alert(Page, $"导出的时间范围不得超过 {dayslimit} 天");
                return;
            }
            string condition = MyWebHelper.SelectKey(pnlSearch, out _);
            string key = Guid.NewGuid().ToString().Replace("-", "");
            CacheHelper.Instance.AddObject(key, condition);
            string url = $"http://{Request.Url.Host}:{Request.Url.Port}/Export.aspx?Guid={key}&Mode={(int) mode}";
            JScript.RedirectInNewWindow(Page, url);
        }

        /// <summary>
        /// 翻页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void pager_PageIndexChange(object sender, EventArgs e)
        {
            BindList();
        }
    }
}